package cn.ym.org.service.impl;


import cn.ym.basic.service.impl.BaseServiceImpl;
import cn.ym.basic.util.Md5Utils;
import cn.ym.basic.util.StrUtils;
import cn.ym.org.domain.Employee;
import cn.ym.org.mapper.EmployeeMapper;
import cn.ym.org.service.IEmployeeService;
import cn.ym.user.domain.Logininfo;
import cn.ym.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * service实现类:员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void add(Employee employee) {

        //一.处理盐值和密码
        //盐值36位
        String salt = StrUtils.getComplexRandomString(32);
        //加密加盐
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        //设置盐值
        employee.setSalt(salt);
        //设置密码
        employee.setPassword(md5Pwd);

        //二.创建Logininfo对象
        Logininfo logininfo = new Logininfo();
        //BeanUtils根据属性同名原则copy属性值
        BeanUtils.copyProperties(employee,logininfo);
        //设置type类型属性为0
        logininfo.setType(0);//0-管理员
        //设置状态为true：启用
        logininfo.setDisable(true);

        //三.将Logininfo对象添加到数据库，并生成自增id
        //生成自增长id
        logininfoMapper.add(logininfo);

        //四.将logininfo_id设置到employee对象中
        employee.setLogininfo_id(logininfo.getId());

        //五.添加到数据库
        super.add(employee);

        //六.为当前员工添加角色信息
        if (employee.getRole_id() != null){
            employeeMapper.addRole(employee);
        }

    }
    @Transactional
    @Override
    public void delete(Long id) {
     /*
        外键：关联字段 + 外键约束
        如果只有关联字段，没有约束可以随便删
        但是如果有关联字段，也有外键约束 - 先删除关联信息，再删除当前表的数据
        开发中：先添加关联字段，不添加外键约束。开发完成之后将约束条件加进去。-方便开发时测试
     */
        //删除角色信息
        employeeMapper.delEmployeeRoleByEmployeeId(id);


        //1.查询employee对象
        Employee employee = employeeMapper.findOne(id);
        //2.删除logininfo信息
        logininfoMapper.delete(employee.getLogininfo_id());
        //3.删除employee信息
        super.delete(id);
    }
    @Transactional
    @Override
    public void update(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //查询到原来的employee对象
        Employee oldEmployee = employeeMapper.findOne(employee.getId());
        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());

        //更新数据t_employee
        super.update(employee);

        //更新数据t_logininfo
        Logininfo oldLogininfo = logininfoMapper.findOne(oldEmployee.getLogininfo_id());
        BeanUtils.copyProperties(employee,oldLogininfo);
        //属性同名原则会把员工的id拷贝给登录信息对象的id
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        logininfoMapper.update(oldLogininfo);

        //修改当前员工的角色
        if(employee.getRole_id()!=null){
            //删除角色信息
            employeeMapper.delEmployeeRoleByEmployeeId(employee.getId());
            //再添加新的角色信息
            employeeMapper.addRole(employee);
        }

    }

    //批量删除
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    /**
     * 查询接单人
     * @param shopId
     * @return
     */
    @Override
    public List<Employee> loadByShopId(Long shopId) {
        return employeeMapper.loadByShopId(shopId);
    }

}
